package com.fang.algorithm.zuo.basis;

/**
 * @author fanglingxiao
 * @version 1.0
 * @description 通过二分法查找满足 >= value 的最左的位置
 * @date 2022/7/30 6:19 下午
 **/
public class BinarySearch2 {
    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 3, 3, 3, 4, 5, 6, 9, 11, 12, 14};
        int num = 5;
        int index = queryIndex(arr, num);
        System.out.println(index);
    }

    private static int queryIndex(int[] arr, int num) {
        int index = -1;
        if (arr == null) {
            return index;
        }
        int L = 0, R = arr.length - 1;
        while (L <= R) {
            int mid = L + ((R - L) >> 1);
            if (arr[mid] >= num) {
                index = mid;
                R = mid - 1;
            } else {
                L = mid + 1;
            }
        }
        return index;
    }

}
